package manong.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import manong.beans.lesson;
import manong.beans.manager;
import manong.beans.student;
import manong.beans.teacher;
import manong.service.lessonService;
import manong.service.managerService;
import manong.service.studentService;
import manong.service.teacherService;

@Controller 
@RequestMapping("/manong")
public class manongController {
     @Resource
    managerService managerservice;
     @Resource
  	lessonService lessonservice;
     
     @Resource
     teacherService teacherservice;
     @Resource
     studentService studentservice;
     @RequestMapping("/main")
     public String main() {
    	 return "main";
     }
     @RequestMapping("/login")
     public String login() {
    	 return "login";
     }
     @RequestMapping("/gotoLogin")
     public String gotoLogin(String username,String password,String work,ModelMap m) {
    	 if(work.equals("student")) {
    	 student students=studentservice.login1(username,password);
    	 if(students!=null) {
    		 return "lesson-manager";
    	 }
    	 else {
    		 m.put("msg", "密码输入错误请重新输入！");
    		 return "login";
    	 }
    	 }
    	 else if(work.equals("teacher")) {
    		 return "";
    	 }
    	 else if(work.equals("manager")) {
    		 manager managers=lessonservice.login(username, password);
        	 if(managers!=null) {
        		 m.put("manager" ,managers);
        		 return "managermain";
        	 }
        	 else {
        		 m.put("msg", "密码输入错误请重新输入！");
        		 return "login";
        	 }
 }
    	 return "login";
     }
     @RequestMapping("/managermain")
public String managermain() {
    	 return "common/managermain";
     }
     @RequestMapping("/getManager")
     public String getManager(String username,ModelMap m) {
    	 manager mana=lessonservice.get_manager(username);
    	 m.put("manager", mana);
    	 return "manager";
     }
     
   /*  
     @GetMapping("types/input")
     public String typesInput(Model model) {
         model.addAttribute("type", new Type());
         return "/admin/types-input";
     }
*/
     
     
 	@RequestMapping("/getlessonAll")
 	public String getAll(ModelMap m)
 	{
 		List<lesson> list = lessonservice.getAll_lesson();
 		m.put("list",list);
 		return "lesson-manager"; 
 	}
 	@RequestMapping("/getlesson")
 	public String getlesson(int cno,ModelMap m) {
 		lesson lesson=lessonservice.get_lesson(cno);
 		m.put("lesson", lesson);
 		return "lesson-update";
 	}
 	@RequestMapping("/update")
 	public String update(int cno,ModelMap m )
 	{
 		lesson lesson = lessonservice.get_lesson(cno);
 		m.put("lesson", lesson);
 		return "lesson-update";		
 	}
 	//真正的提交,进行修改
 	@RequestMapping("/gotoUpdate")
 	public String gotoUpdate(lesson lesson,ModelMap m ) 
 	{	
 		lessonservice.update_lesson(lesson);
 		m.put("msg", "课表修改成功");
 		return "lesson-update";
 	}
 	@RequestMapping("/deleteLesson")
 	public String delete(int cno,ModelMap m)
 	{
 		lessonservice.delete_lesson(cno);
 		return "redirect:/manong/getlessonAll";
 	}
 	//添加
 	@RequestMapping("/add")
 	public String add()
 	{
 		return "lesson-add";  
 	}
 	@RequestMapping("/gotoAdd")
 	public String gotoAdd(lesson lesson,ModelMap m) {
 		lessonservice.add_lesson(lesson);
 		m.put("msg", "添加成功!");
 		return "lesson-add";
 	}
 	
 	//教师
 	@RequestMapping("/getTeacherAll")
 	public String getTeacherAll(ModelMap m) {
 		List<teacher> list=teacherservice.getAll_teacher();
 		m.put("teacherList", list);
 		return "teacher-manager";
 	}
 	@RequestMapping("/updateTeacher")
 	public String updateTeacher(int eno,ModelMap m) {
 		teacher teacher =teacherservice.get_teacher(eno);
 		m.put("teacher", teacher);
 		return "teacher-update";
 	}
 	@RequestMapping("/gotoUpdateTeacher")
 	public String gotoUpdateTeacher(teacher teacher,ModelMap m) {
 		teacherservice.update_teacher(teacher);
 		m.put("teacher", teacher);
 		m.put("msg", "修改成功");
 		return "teacher-update";
 	}
 	@RequestMapping("/deleteTeacher")
 	public String deleteTeacher(int eno) {
 		teacherservice.delete_teacher(eno);
 		return "redirect:/manong/getTeacherAll";
 	}
 	@RequestMapping("/addTeacher")
 	public String addTeacher () {
 		return "teacher-add";
 	}
 	@RequestMapping("/gotoAddTeacher")
 	public String addTeacher(teacher teacher,ModelMap m) {
 		teacherservice.add_teacher(teacher);
 		m.put("msg", "添加成功");
 		return "teacher-add";
 	}
 	
 	//学生
 	@RequestMapping("/getStudentAll")
 	public String getStudentAll(ModelMap m) {
 		List<student> list=studentservice.getAll_student();
 		m.put("studentList", list);
 		return "student-manager";
 		
 	}
 	@RequestMapping("/addStudent")
 	public String addStudent() {
 		return "student-add";
 	}

 	@RequestMapping("/gotoAddStudent")
 	public String gotoAddStudent(student student,ModelMap m) {
 		studentservice.add_student(student);
 		m.put("msg", "添加成功");
 		return "student-add";
 	}
 	
 	@RequestMapping("/updateStudent")
 	public String updateStudent(int sno,ModelMap m) {
 		student stu=studentservice.get_student(sno);
 		m.put("student", stu);
 		return "student-update";
 		
 	}
 	
 	@RequestMapping("/gotoUpdateStudent")
	public String gotoUpdateStudent(student student,ModelMap m) {
 		studentservice.update_student(student);
 		m.put("msg", "修改成功");
 		return "student-update";
 	}
 	@RequestMapping("/deleteStudent")
 	public String deleteStudent(int sno,ModelMap m) {
 		studentservice.delete_student(sno);
 		return "redirect:/manong/getStudentAll";
 		
 	}
 	
}
